import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useCourseStore = defineStore('course',() => {
    // 模拟课程数据
    const courses = ref([
        {
            id: 1,
            courseName: "动漫中级",
            duration: 90,
            capacity: 8,
            consumeTimes: 1,
            supportMemberCards:[1, 2],
            description: "",
            genderConstraint: "无",
            ageConstraint: 10,
            reservationConstraint: 2
        },
        {
            id: 2,
            courseName: "动漫高级",
            duration: 120,
            capacity: 6,
            consumeTimes: 3,
            supportMemberCards:[1],
            description: "各种风格的动漫",
            genderConstraint: "无",
            ageConstraint: 13,
            reservationConstraint: 2
        },
    ])

    const getCourseById = (id) => {
        return courses.value.find(item => item.id === id)
    }

    const addCourse = (course) => {
        courses.value.push({
            id: courses.value.length + 1,
            courseName: course.courseName,
            duration: course.duration,
            capacity: course.capacity,
            consumeTimes: course.consumeTimes,
            supportMemberCards: course.supportMemberCards,
            description: course.description,
            genderConstraint: course.genderConstraint,
            ageConstraint: course.ageConstraint,
            reservationConstraint: course.reservationConstraint
        });
    }

    const editCourse = (course) => {
        const temp = courses.value.find(item => item.id === course.id)
        temp.courseName = course.courseName
        temp.duration = course.duration
        temp.capacity = course.capacity
        temp.consumeTimes = course.consumeTimes
        temp.supportMemberCards = course.supportMemberCards
        temp.description = course.description
        temp.genderConstraint = course.genderConstraint
        temp.ageConstraint = course.ageConstraint
        temp.reservationConstraint = course.reservationConstraint
    }

    const deleteCourse = (id) => {
        const index = courses.value.findIndex(item => item.id === id)
        courses.value.splice(index, 1)
    }

    return {courses,
        getCourseById,
        addCourse,
        editCourse,
        deleteCourse
    }
})
